package com.platform.usercenter.domain;

import cn.hutool.core.collection.CollectionUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.platform.common.core.annotation.Excel;
import com.platform.common.core.web.domain.BaseEntity;
import com.platform.constants.SysUserConstants;
import com.platform.usercenter.service.TreeAdapter;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 组织部门对象 sys_organization_dept
 *
 * @author platform
 * @date 2024-10-24
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class SysOrganizationDept extends BaseEntity implements TreeAdapter {

    /**
     * 部门id
     */
    private Long deptId;

    /**
     * 组织ID
     */
    @Excel(name = "组织ID")
    private Long orgId;

    /**
     * 父部门id
     */
    @Excel(name = "父部门id")
    private Long parentId;

    /**
     * 部门名称
     */
    @Excel(name = "部门名称")
    private String deptName;

    /**
     * 显示顺序
     */
    @Excel(name = "显示顺序")
    private Long deptSort;

    /**
     * 负责人
     */
    @Excel(name = "负责人")
    private String leader;

    /**
     * 联系电话
     */
    @Excel(name = "联系电话")
    private String phone;

    /**
     * 邮箱
     */
    @Excel(name = "邮箱")
    private String email;

    /**
     * 部门状态（0正常 1停用）
     */
    @Excel(name = "部门状态", readConverterExp = "0=正常,1=停用")
    private String status;

    /**
     * 部门类型：0=单位，1=部门，2=科室
     */
    @Excel(name = "部门类型：0=单位，1=部门，2=科室")
    private String type;

    /**
     * 是否删除(Y,N)
     */
    @Excel(name = "是否删除(Y,N)")
    private String isDelete;

    /**
     * 删除人
     */
    @Excel(name = "删除人")
    private String deleteBy;

    /**
     * 删除时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "删除时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date deleteTime;

    /**
     * 子部门列表
     */
    private List<SysOrganizationDept> children = new ArrayList<>();

    /**
     * 岗位列表
     */
    private List<SysDeptPost> sysDeptPosts;
    /**
     * 类型 1-组织 2-部门 3-岗位
     */
    private String treeType = SysUserConstants.TYPE_DEPT;

    @Override
    public Long getTreeCurrentId() {
        return deptId;
    }

    @Override
    public Long getTreeParentId() {
        return parentId;
    }

    @Override
    public <T> void setTreeChildren(List<TreeAdapter> list, Class<T> clazz) {
        if (this.getClass().isAssignableFrom(clazz) && CollectionUtil.isNotEmpty(list)) {
            List<SysOrganizationDept> collect = list.stream().map(SysOrganizationDept.class::cast).collect(Collectors.toList());
            this.setChildren(collect);
        }
    }


}
